﻿using System.Windows;
using System.Windows.Input;
using IS_Ever_Green.DataAccess.Model;
using IS_Ever_Green.DataAccess.Model.Repositories;
using IS_Ever_Green.View;

namespace IS_Ever_Green.ViewModel
{
    public class RegistrationViewModel : BaseViewModel
    {
        private IUserRepository userRepository;
        private string _firstName;
        private string _lastName;
        private string _login;
        private string _email;
        private string _password;
        private string _passwordRepeat;

        private string _errorMessageLogin;
        private string _errorMessageEmail;
        private string _errorMessagePassword;

        public string FirstName 
        {
            get { return _firstName; }
            set
            {
                _firstName = value;
                OnPropertyChanged(nameof(FirstName));
            }
        }

        public string LastName
        {
            get { return _lastName; }
            set
            {
                _lastName = value;
                OnPropertyChanged(nameof(LastName));
            }
        }

        public string Login
        {
            get { return _login; }
            set
            {
                _login = value;
                OnPropertyChanged(nameof(Login));
            }
        }

        public string Email
        {
            get { return _email; }
            set
            {
                _email = value;
                OnPropertyChanged(nameof(Email));
            }
        }

        public string Password
        {
            get { return _password; }
            set
            {
                _password = value;
                OnPropertyChanged(nameof(Password));
            }
        }

        public string PasswordRepeat
        {
            get { return _passwordRepeat; }
            set
            {
                _passwordRepeat = value;
                OnPropertyChanged(nameof(PasswordRepeat));
            }
        }

        public string ErrorMessageLogin
        {
            get { return _errorMessageLogin; }
            set
            {
                _errorMessageLogin = value;
                OnPropertyChanged(nameof(ErrorMessageLogin));
            }
        }

        public string ErrorMessageEmail
        {
            get { return _errorMessageEmail; }
            set
            {
                _errorMessageEmail = value;
                OnPropertyChanged(nameof(ErrorMessageEmail));
            }
        }

        public string ErrorMessagePassword
        {
            get { return _errorMessagePassword; }
            set
            {
                _errorMessagePassword = value;
                OnPropertyChanged(nameof(ErrorMessagePassword));
            }
        }


        public ICommand RegistrationCommand { get; }

        public RegistrationViewModel()
        {
            userRepository = new UserRepository();
            RegistrationCommand = new CommandViewModel(ExecuteRegistrationCommand, CanExecuteRegistrationCommand);
        }

        private bool CanExecuteRegistrationCommand(object obj)
        {
            return !string.IsNullOrWhiteSpace(FirstName) && FirstName.Length >= 3 &&
                   !string.IsNullOrWhiteSpace(LastName) && LastName.Length >= 3 &&
                   !string.IsNullOrWhiteSpace(Login) && Login.Length >= 3 &&
                   !string.IsNullOrWhiteSpace(Email) && Email.Length >= 3 &&
                   !string.IsNullOrWhiteSpace(Password) && Password.Length >= 3 && 
                   !string.IsNullOrWhiteSpace(PasswordRepeat) && PasswordRepeat.Length >= 3;
        }

        private void ExecuteRegistrationCommand(object obj)
        {
            ErrorMessageLogin = "";
            ErrorMessageEmail = "";
            ErrorMessagePassword = "";

            ValidateAndSetErrors(Login, Email, Password, PasswordRepeat);

            if (!string.IsNullOrEmpty(ErrorMessageEmail) || !string.IsNullOrEmpty(ErrorMessagePassword) || !string.IsNullOrEmpty(ErrorMessageLogin))
            {
                return;
            }

            var user = new UserModel();
            user.FirstName = FirstName;
            user.LastName = LastName;
            user.LoginUser = Login;
            user.Email = Email;
            user.Password = Password;
            user.RoleId = 2;

            userRepository.RegisterUser(user);

            OpenLoginView();
        }


        private void ValidateAndSetErrors(string login, string email, string password, string passwordRepeat)
        {
            if (userRepository.UserExists(login))
            {
                ErrorMessageLogin = "Логин занят";
            }
            if (!email.Contains("@"))
            {
                ErrorMessageEmail = "Некорректный адрес электронной почты.";
            }

            if (password != passwordRepeat)
            {
                ErrorMessagePassword = "Пароли не совпадают";
            }
        }


        private void OpenLoginView()
        {
            var loginView = new LoginView();
            loginView.Show();
            var registrationView = Application.Current.Windows.OfType<RegistrationView>().FirstOrDefault();
            if (registrationView != null)
            {
                registrationView.Close();
            }
        }
    }
}
